<?php
class UserAction extends CommonAction{
	




	public function index(){
		//判断是否已登录
		$user = $this->getLoginUser();
		$this->user = $user;
		$this->display();
	}
	
	//用户登录
	public function login(){
		if(IS_POST){
			$data = array('status' => 0,'msg' => '未知错误');
			$type = I("type","pass",'trim');
			if($type == "pass"){//密码方式登录
				$password = I("password",'','trim');
				$phone = I("phone",'','trim');
				if(!checkphone($phone)){
					$data['msg'] = "手机号码不符合规范";
				}else{
					$password = sha1(md5($password));
					$User = D("user");
					//echo $phone;
					//echo "<br>";
					//echo $password;
					//exit;
					$info = $User->where(array('phone' => $phone,'password' => $password))->find();
					if(!$info){
						$data['msg'] = "帐户名或密码错误";
					}else if($info['status'] != 1){
						$data['msg'] = "该账户已被禁止登录!";
					}else{
						$this->setLoginUser($phone);
						$data['status'] = 1;
					}
				}
			}else{//短信验证码登录
				$phone = I("phone",'','trim');
				$code = I("code",'','trim');
				$User = D("user");
				$Smscode = D("smscode");
				//判断手机号
				if(!checkphone($phone)){
					$data['msg'] = "手机号不符合规范";
				}elseif(strlen($code) != 4){
					$data['msg'] = "短信验证码输入有误";
				}else{
					//判断验证码是否正确
					$info = $Smscode->where(array('phone' => $phone))->order("sendtime desc")->find();
					
					if(!$info || $info['code'] != $code){
						$data['msg'] = "短信验证码输入有误";
					}elseif( (time()-30*60) > $info['sendtime']){
						$data['msg'] = "验证码已过期,请重新获取!";
					}else{
						//判断用户是否存在
						$count = $User->where(array('phone' => $phone))->count();
						if(!$count){
							$data['msg'] = "用户不存在,请先注册!";
						}else{
							$this->setLoginUser($phone);
							$data['status'] = 1;
						}
					}
				}
			}
			$this->ajaxReturn($data);
			exit;
		}
		//判断是否已登录
		if($this->getLoginUser()){
			$this->redirect('User/index');
		}
		$this->display();
	}
	
	//注销登陆
	public function logout(){
		$this->setLoginUser('');
		$this->redirect('User/login');
	}
	
	//用户注册
	public function signup(){
		if(IS_POST){
			$User = D("user");
			$data=array('status' => 0,'msg' => '未知错误');
			$password = I("password",'','trim');
			$code = I("code",'','trim');
			$phone = I("phone",'','trim');
			$yao_phone = I("yao_phone",'','trim');
			$yaoqingma=I("yaoqingma");
			
			//再次验证手机号
			if(!checkphone($phone)){
				$data['msg'] = "手机号不符合规范!";
			}elseif(strlen($password) < 6 || strlen($password) > 16){
				$data['msg'] = "请输入6-16位密码!";
			}else{
				$count = $User->where(array('phone' => $phone))->count();
				if($count){
					$data['msg'] = "手机号已注册,请登录!";
					$this->ajaxReturn($data);exit;
				}
				
				$chk_yaoqingma=M("yaoqingma")->where("code='{$yaoqingma}' and status=0")->find();
				if(empty($chk_yaoqingma)){
					$data['msg'] = '填写的邀请码不正确';
					$this->ajaxReturn($data);exit;
				}
				
				//验证短信验证码
				//$Smscode = D("Smscode");
				//$info = $Smscode->where(array('phone' => $phone))->order("sendtime desc")->find();
				//if(!$info || $info['code'] != $code){
				//	$data['msg'] = "短信验证码有误!";
				//}elseif( (time()-30*60) > $info['sendtime']){
				//	$data['msg'] = "验证码过时,请重新获取!";
				if(1==2){
				}else{
					$password = sha1(md5($password));
					$invitation = '';
					while(true){
						$str = 'qwertyuiopasdfghjklzxcvbnm';
						$invitation = $str[rand(0, strlen($str) - 1)];
						$invitation = $invitation.rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9);
						$result = $User->where(['invitation'=>$invitation])->find();
						if (!$result) {
							break;
						}
					}
					$arr = array(
						'phone' => $phone,
						'password' => $password,
						'yao_phone' => $yao_phone,
						'invitation' => $invitation,
						'addtime' => time(),
						'yaoqingma'=>$yaoqingma
					);
					
					//更新邀请码状态
					$data_yqm['use_time']=time();
					$data_yqm['use_user']=$phone;
					$data_yqm['status']=1;
					$ret=M("yaoqingma")->where("code='{$yaoqingma}'")->save($data_yqm);
					
					$status = $User->add($arr);
					if($status){
						//设置当前登录用户
						$this->setLoginUser($phone);
						$data['status'] = 1;
					}else{
						$data['msg'] = "注册账户失败!";
					}
				}
			}
			$this->ajaxReturn($data);
			exit;
		}
		$this->display();
	}
	
	//发送验证码
	public function sendsmscode(){
		$data = array('status' => 0);
		$phone = I("phone",'','trim');
		$type = I("type","login",'trim');
		if($type == "reg"){
			$User = D("user");
			$count = $User->where(array('phone' => $phone))->count();
			if($count){
				$data['msg'] = "手机号已注册,请登录!";
				$this->ajaxReturn($data);exit;
			}
		}
		$verifycode = I("verifycode",'','trim');
		if(!checkphone($phone)){
			$data['msg'] = "手机号不规范";
		}else{
			if($_SESSION['verify'] != md5($verifycode) && $type != "zhima"){
				$data['msg'] = "图形验证码错误";
			}else{
				//判断发送次数
				$Maxcount = C('cfg_smsmaxcount');
				$Maxcount = intval($Maxcount);
				//if(!$Maxcount){
					$Maxcount = 5000;
				//}
				$todaytime = strtotime(date("Y-m-d"));
				$Code = D("smscode");
				$c = rand(1111,9999);
				$content = C('cfg_smssign');
				$content = '【'.$content."】您的验证码为{$c}，请于5分钟内正确输入，如非本人操作，请忽略此短信。";
				/*
				$smsapi = "http://api.smsbao.com/";
				$user = C('cfg_smssid'); //短信平台帐号
				$pass = md5(C('cfg_smstoken')); //短信平台密码
				
								
				$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);

				$result =file_get_contents($sendurl) ;
				*/
				
				//吉信通
				$uid=C('cfg_smssid'); //分配给你的账号
				$pwd=C('cfg_smstoken'); //密码	
				
				$contents=urlencode(auto_charset($content,"utf-8",'gbk'));  //短信内容	
				$sendurl="http://service.winic.org/sys_port/gateway/?id=".$uid."&pwd=".$pwd."&to=".$phone."&content=".$contents;
				//echo $sendurl;exit;
				$result =file_get_contents($sendurl) ;
				$datas = explode("/",$result);
				if($datas[0]=='000'){
					$result=0;
				}
				
				if($result=='0'){
					//写入验证码记录
					$Code->add(array(
						'phone'    => $phone,
						'code'     => $c,
						'sendtime' => time()
					));
					$data['status'] = 1;							
				}else{
					$data['status'] = 0;
					$data['msg'] = "验证码发送失败,错误码:".$status;
				}

			
			}
		}
		$this->ajaxReturn($data);
	}

	//找回密码
	public function backpwd(){
		if(IS_POST){
			$User = D("user");
			$data=array('status' => 0,'msg' => '未知錯誤');
			$password = I("password",'','trim');
//			$code = I("code",'','trim');
			$phone = I("phone",'','trim');
            $yaoqingma = I("yaoqingma",'','trim');
            $verifycode = I("verifycode",'','trim');

			//再次验证手机号
			if(!checkphone($phone)){
				$data['msg'] = "手機號不符合規範!";
			}elseif(strlen($password) < 6 || strlen($password) > 16){
				$data['msg'] = "請輸入6-16位密碼!";
			}else{
				$count = $User->where(array('phone' => $phone))->count();
				if(!$count){
					$data['msg'] = "該賬戶還沒有註冊,請先註冊!";
					$this->ajaxReturn($data);exit;
				}else{
					if($_SESSION['verify'] != md5($verifycode)){
                        $data['msg'] = "圖形驗證碼錯誤";
					}else{
					    $user = $User->where(array('phone' => $phone))->find();

					    if($user['yaoqingma'] == $yaoqingma){
                            $password = sha1(md5($password));
                            $arr = array('password' => $password);
                            $status = $User->where(array('phone' => $phone))->save($arr);
                            if(is_int($status)){
                                $data['status'] = 1;
                                $data['msg'] = "修改密碼成功!";
                            }else{
                                $data['msg'] = "修改密碼失敗!";
                            }
                        }else{
                            $data['msg'] = "邀請碼錯誤!";
                        }
					}
				}
			}
			$this->ajaxReturn($data);
		}
		$this->display();
	}

	//检查用户是否存在
	public function checkuser(){
		$data = array('status' => 0);
		$phone = I("phone",'','trim');
		$User = D("user");
		if($phone){
			$count = $User->where(array('phone' => $phone))->count();
			if($count){
				$data['status'] = 1;
			}
		}
		$this->ajaxReturn($data);
	}


	//初审
	public function chushen(){
		//判断是否已登录
		$user = $this->getLoginUser();
		if(!$user){
			$this->redirect('User/login');
		}

		$data= M('userinfo')->where(['user'=>$this->getLoginUser()])->find();
		$this->liht=$data;

		$this->user = $user;
		$this->display();
	}




}
